

#import "FAMMineLastAndTargetWeightViewController.h"

@interface FAMMineLastAndTargetWeightViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *FAM_lbsButton;
@property (weak, nonatomic) IBOutlet UIButton *FAM_kgButton;

@property (nonatomic, strong)NSMutableArray *FAMLbsArray,*FAMKgArray;
@property (weak, nonatomic) IBOutlet UIPickerView *FAMPickerView;

@property (nonatomic,strong)NSString *FAMWeight;


@end

@implementation FAMMineLastAndTargetWeightViewController
- (NSMutableArray *)FAMLbsArray{
    if (!_FAMLbsArray) {
        _FAMLbsArray = [NSMutableArray array];
        for (int i = 60; i<= 400; i++) {
            [_FAMLbsArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMLbsArray;
}
- (NSMutableArray *)FAMKgArray{
    if (!_FAMKgArray) {
        _FAMKgArray = [NSMutableArray array];
        for (int i = 30; i<= 200; i++) {
            [_FAMKgArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMKgArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self FAM_showUi];
}
- (void)FAM_showUi{
    self.FAMPickerView.delegate = self;
    self.FAMPickerView.dataSource = self;
    if (self.FAM_kgButton.isSelected) {
        [self.FAMPickerView selectRow:30 inComponent:0 animated:YES];
    }else{
        [self.FAMPickerView selectRow:60 inComponent:0 animated:YES];
    }
    if (self.FAMIsTarget) {
        self.FAMWeight = [FAMUserManger shareInstance].userModel.weight;
    }else{
        self.FAMWeight = ((FAMWeightsModel *)[FAMUserManger shareInstance].userModel.weights.lastObject).weight;
    }
    
}
- (IBAction)FAM_lbsButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAM_kgButton.backgroundColor = kClearColor;
    [self.FAM_kgButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAM_kgButton setSelected:NO];
    [self.FAMPickerView reloadAllComponents];
    [self.FAMPickerView selectRow:60 inComponent:0 animated:YES];
}

- (IBAction)FAM_kgButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAM_lbsButton.backgroundColor = kClearColor;
    [self.FAM_lbsButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAM_lbsButton setSelected:NO];
    [self.FAMPickerView reloadAllComponents];
    [self.FAMPickerView selectRow:30 inComponent:0 animated:YES];
}
#pragma mark ---- UIPickerViewDelegate UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.FAM_kgButton.isSelected) {
        return self.FAMKgArray.count;
    }else{
        return self.FAMLbsArray.count;
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (self.FAM_kgButton.isSelected) {
        return [NSString stringWithFormat:@"%@kg",self.FAMKgArray[row]];
    }else{
        return [NSString stringWithFormat:@"%@lbs",self.FAMLbsArray[row]];
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.FAM_kgButton.isSelected) {
        self.FAMWeight = self.FAMKgArray[row];
    }else{
        NSInteger templbs = ((NSString *)self.FAMLbsArray[row]).integerValue;
        float weight = templbs * lbs;
        self.FAMWeight = [NSString stringWithFormat:@"%0.2f",weight];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 60;
}
- (IBAction)FAMSaveButtonClick:(UIButton *)sender {
    WeakSelf(self);
    [self showHudInKeyWindowWithHint:nil];
    NSString *weight;
    if (self.FAMIsAmend) {
        weight = self.FAMAmendWeight;
    }else{
        if (self.FAMIsTarget) {
            weight = @"target_weight";
        }else{
            weight = @"weight";
        }
    }
    [FAMHttpTool FAM_PutFoodmateUserdataWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":@{weight:self.FAMWeight}}] success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            if (self.FAMIsAmend) {
                if ([weight isEqualToString:@"init_weight"]) {
                    [FAMUserManger shareInstance].userModel.inits_weight = weakSelf.FAMWeight;
                }else if ([weight isEqualToString:@"weight"]){
                    [FAMUserManger shareInstance].userModel.weight = weakSelf.FAMWeight;
                }else{
                    [FAMUserManger shareInstance].userModel.target_weight = weakSelf.FAMWeight;
                }
            }else{
                if (weakSelf.FAMIsTarget) {
                    [FAMUserManger shareInstance].userModel.target_weight = weakSelf.FAMWeight;
                }else{
                    ((FAMWeightsModel *)[FAMUserManger shareInstance].userModel.weights.lastObject).weight = weakSelf.FAMWeight;
                }
            }
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            [weakSelf showHint:errorInfo];
        });
    }];
}

@end
